package com.AIDS.AIDSweeper.server.model.util;

import java.util.Arrays;

import javax.jdo.PersistenceManager;

import com.AIDS.AIDSweeper.server.model.Player;
import com.AIDS.AIDSweeper.server.model.DAO.GoogleDAOFactory;

public final class AIDSutils {

	private static final AIDSutils Instance = new AIDSutils();;
	
	public final String GAME_STATUS_OVER = "gameOver";
	public final String GAME_STATUS_ON = "gameOn";
	public final String GAME_STATUS_READY = "gameRdy";
	
	public final String PLAYER_STATUS_OFFLINE = "playerOffline";
	public final String PLAYER_STATUS_ONLINE = "playerOnline";
	public final String PLAYER_STATUS_PLAYING = "playerPlaying";
	
	public final String GAMER_NOT_READY = "gamerNotRdy";
	public final String GAMER_READY = "gamerRdy";
	public final String GAMER_PLAYING = "gamerPlaying";
	public final String GAMER_QUIT = "gamerQuit";
	
	public final String ROLE_MINER = "miner";
	public final String ROLE_SWEEPER = "sweeper";
	
	public final String SESSION_EMPTY = "";
	
	public final int STARTING_SCORE = 0;
	
	private AIDSutils (){}
	
	public static AIDSutils getInstance(){
		return Instance;
	}
	
	public static boolean checkSession(String session) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Player player = GoogleDAOFactory.instance().getPlayerDAO().findBySession(session,pm);
		pm.close();
		if (player != null || session == Instance.SESSION_EMPTY)
			return true;
		return false;
	}
	
	public static String hasher(String strToHash) {
		
		int[] hash = {0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19};

		int iteration = 64;

		int[] k = {
		   0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
		   0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
		   0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
		   0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
		   0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
		   0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
		   0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
		   0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};

		int l = strToHash.length() / 64;

		if (64 - (strToHash.length() % 64) >= 8) {
			l++;
		} else {
			l+=2;
		}

		int Ls = l*64;

		byte[] bigChunkMessage = new byte[Ls];

		for(int i = 0; i<strToHash.length();i++)
			bigChunkMessage[i] = strToHash.getBytes()[i];

		bigChunkMessage[strToHash.length()] |= 0x80;


		Arrays.fill(bigChunkMessage,strToHash.length()+1,Ls-8,(byte)0x00);

		int i = strToHash.length()*8;

		bigChunkMessage[Ls-4] = (byte)(i >> 24);
		bigChunkMessage[Ls-3] = (byte)(i >> 16);
		bigChunkMessage[Ls-2] = (byte)(i >> 8);
		bigChunkMessage[Ls-1] = (byte)(i);
		bigChunkMessage[Ls-8] = 0x00;
		bigChunkMessage[Ls-7] = 0x00;
		bigChunkMessage[Ls-6] = 0x00;
		bigChunkMessage[Ls-5] = 0x00;

		int[] smallChunks = new int[l*16];
		
		for(i = 0;i < l*16;i++) {
			smallChunks[i] |= (bigChunkMessage[i*4] << 24)& 0xff000000 ;
			smallChunks[i] |= bigChunkMessage[1+i*4] << 16 & 0xff0000;
			smallChunks[i] |= bigChunkMessage[2+i*4] << 8 & 0xff00;
			smallChunks[i] |= (bigChunkMessage[3+i*4] & 0xff);
		}

		for(i=0;i<l;i++) {
			int[] W = new int[64];
			for(int p=0;p<16;p++) {
				W[p] = smallChunks[p+i*16];
			}
			
			for (int t = 16; t < iteration; t++) {
				W[t] = lf_delta1(W[t-2]) + W[t-7] + lf_delta0(W[t-15])
					+ W[t-16];
			}
		   
			int a = hash[0];
			int b = hash[1];
			int c = hash[2];
			int d = hash[3];
			int e = hash[4];
			int f = hash[5];
			int g = hash[6];
			int h = hash[7];

			for (int o = 0; o < iteration; o++) {
				int T1 = h + lf_sigma1(e) + lf_ch(e,f,g) + k[o]+ W[o];
				int T2 = lf_sigma0(a) + lf_maj(a,b,c);
				h = g;
				g = f;
				f = e;
				e = d + T1;
				d = c;
				c = b;
				b = a;
				a = T1 + T2;
			}
			hash[0] += a;
			hash[1] += b;
			hash[2] += c;
			hash[3] += d;
			hash[4] += e;
			hash[5] += f;
			hash[6] += g;
			hash[7] += h;
		}

		return Integer.toHexString(hash[0])+Integer.toHexString(hash[1])+Integer.toHexString(hash[2])
			+Integer.toHexString(hash[3])+Integer.toHexString(hash[4])+Integer.toHexString(hash[5])
			+Integer.toHexString(hash[6])+Integer.toHexString(hash[7]);
	}
		

	private static int lf_ch(int x, int y, int z) {
		return (x & y) ^ ((~x) & z);
	}
	
	
	private static int lf_maj(int x, int y, int z) {
	    return (x & y) ^ (x & z) ^ (y & z);
	}
	
	private static int lf_R( int x, int s ) {
		return (x >>> s);
	}
	
	private static int lf_S(int x, int s) {
		return (x >>> s) | (x << (32 - s));
	}
	
	private static int lf_sigma0(int x) {
		return lf_S(x, 2) ^ lf_S(x, 13) ^ lf_S(x, 22);
	}
	
	private static int lf_sigma1(int x) {
		return lf_S( x, 6 ) ^ lf_S( x, 11 ) ^ lf_S( x, 25 );
	}
	
	private static int lf_delta0(int x) {
		return lf_S(x, 7) ^ lf_S(x, 18) ^ lf_R(x, 3);
	}
	
	private static int lf_delta1(int x) {
		return lf_S(x, 17) ^ lf_S(x, 19) ^ lf_R(x, 10);
	}
}
